package com.eveinfo.display.planner;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;


public class PlannersAdapter extends BaseAdapter {

	private final Context context;
	private final PlannerManager plannerManager;
	private final int characterID;

	public PlannersAdapter(Context context, ListView listView, PlannerManager plannerManager, int characterID) {
		this.context = context;
		this.plannerManager = plannerManager;
		this.characterID = characterID;
		listView.setAdapter(this);
	}

	@Override
	public int getCount() {
		return plannerManager.getSize();
	}

	@Override
	public Object getItem(int position) {
		return plannerManager.getPlanner(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		PlannerRenderer renderer = new PlannerRenderer(context);
		final Planner planner = plannerManager.getPlanner(position);
		renderer.setPlanner(planner);
		renderer.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent myIntent = new Intent(context, PlannerView.class);
				myIntent.putExtra("plannerName", planner.getName());
				myIntent.putExtra("characterID", characterID);
				context.startActivity(myIntent);
			}
		});
		renderer.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				plannerManager.removePlanner(position);
				notifyDataSetChanged();
				return true;
			}
		});
		return renderer;
	}
}
